<template>
  <config-item :label="label" v-if="label != 'cityCode'">
    <el-cascader
      ref="cascader"
      v-model="mValue"
      :options="cascaderList"
      :props="AddrStrictly"
      clearable
      @change="changeCascader"
    ></el-cascader>
  </config-item>
</template>

<script>
import schemaMixin from "@/mixin/schemaMixin";
export default {
  name: "SchemaAddress",
  mixins: [schemaMixin],
  inject: ["control"],
  data() {
    return {
      cascaderList: [],
      AddrStrictly: {
        value: "locationId",
        label: "locationNameZh",
        children: "children",
        checkStrictly: true
      },
      cascaderData: []
    };
  },
  created() {
    this.getData();
  },
  methods: {
    changeCascader() {
      let obj = this.$refs.cascader.getCheckedNodes()[0];
      this.control.curPage.componentList.forEach(j => {
        if (j.component == "McWeather") {
          j.value.city = obj.label;
        }
      });
    },
    getData() {
      this.http
        .get("https://sr.boss.i-town.cn/kuaibao/cityDictory?name=''")
        .then(dic => {
          this.cascaderData = dic.data;
          function convertToTree(regions, rootId = "1") {
            let resArr = [];
            regions.forEach(item => {
              if (item.parentID === rootId) {
                resArr.push(item);
                item.children = convertToTree(regions, item.locationId);
              }
            });
            return resArr;
          }
          this.cascaderList = convertToTree(dic.data);
        });
    }
  }
};
</script>
